﻿using System;
using System.Web.UI.WebControls;
using DMS.Business.Modules.Knowledge;
using DMS.Business.Utility;
using System.Data;

namespace DMS.Web.Modules.Knowledge
{
    public partial class KnowledgeList : System.Web.UI.Page
    {
        public string mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            mode = Request.QueryString["mode"];
            if (!IsPostBack)
            {
                this.InitControls();
                this.InitDataList();
            }
        }

        #region 初始化
        //加载gridView数据
        /// <summary>
        /// 初始化列表
        /// </summary>
        public void InitDataList()
        {
            int total = 0;
            DataSet ds = this.GetDataSource(this.pager.PageSize, this.pager.CurrentPageIndex, out total);

            this.dataList.DataSource = ds;
            this.dataList.DataBind();
            this.pager.RecordCount = total;

            // 处理空数据
            if (total == 0)
            {
                this.dataList.Visible = false;
                this.pager.Visible = false;
                this.dvEmpty.Visible = true;
            }
            else
            {
                this.dataList.Visible = true;
                this.pager.Visible = true;
                this.dvEmpty.Visible = false;
            }
        }

        private DataSet GetDataSource(int pageSize, int pageIndex, out int total)
        {   //设置查询条件
            string condition = "";

            if (!String.IsNullOrEmpty(TitleQ.Text))
                condition = condition + String.Format(" and title like '%{0}%'", (object) TitleQ.Text);

            if (!String.IsNullOrEmpty(PublisherQ.Text))
                condition = condition + String.Format(" and Publisher like '%{0}%'", (object) PublisherQ.Text);

            if (!String.IsNullOrEmpty(PublishTimeQ1.Text))
                condition = condition + String.Format(" and DateDiff(day, PublishTime, Cast('{0}' as datetime)) <= 0", (object) PublishTimeQ1.Text);

            if (!String.IsNullOrEmpty(PublishTimeQ2.Text))
                condition = condition + String.Format(" and DateDiff(day, PublishTime, Cast('{0}' as datetime)) >= 0", (object) PublishTimeQ2.Text);

            total = KnowledgeService.CountKnowledge(condition);

            DataSet ds2 = KnowledgeService.ListKnowledge(pageSize, pageIndex, total, condition);
            
            return ds2;
        }

        //根据需要动态显示操作列
        private void InitControls()
        {
            
        }
        #endregion 初始化结束

        #region 翻页查询
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageChanged(object sender, EventArgs e)
        {
            this.InitDataList();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnQuery_Click(object sender, EventArgs e)
        {
            this.pager.GoToPage(1);
            this.InitDataList();
        }
        
        #endregion

        #region 更新
        protected void OnCommand(object source, DataGridCommandEventArgs e)
        {
            string id = dataList.DataKeys[e.Item.ItemIndex].ToString();
            if (e.CommandName.Equals("Delete"))
            {
                KnowledgeService.DeleteKnowledge(id);
                InitDataList();
            }
            else if (e.CommandName.Equals("Edit"))
            {
                ClientHelper.Instance.RenderScript(Page, "", "editKnowledge('" + id + "')");
            }

        }
        #endregion
    }
}